package com.ysxsoft.wdg.ui.activity.my.jf;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.my.JfGoodsAdapter;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.MyJfBean;
import com.ysxsoft.wdg.bean.response.HomeProductResponse;
import com.ysxsoft.wdg.bean.response.MyChangeResponse;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.ViewUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.ysxsoft.wdg.util.statusbar.StatusBarUtil;
import com.ysxsoft.wdg.util.transform.DividerGridItemDecoration;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

public class MyJfdhActivity extends BaseActivity {

    public static void start(Context context) {
        Intent intent = new Intent(context, MyJfdhActivity.class);
        context.startActivity(intent);
    }

    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.tv_wfh)
    TextView tvWfh;
    @BindView(R.id.tv_yfh)
    TextView tvYfh;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.multStatusView)
    MultipleStatusView multStatusView;

    private JfDuiHuanAdapter adapter;
    private int page = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_jfdh;
    }

    @Override
    protected void initData() {
        StatusBarUtil.setStatusBarTextColor(this, true);
        initStatusBar(topView, true);
        titleContent.setText("我的兑换");
        tvWfh.setSelected(true);

        adapter = new JfDuiHuanAdapter(R.layout.item_jf_exchange);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        recyclerview.addItemDecoration(new DividerGridItemDecoration(mContext, 1, R.color.line_color));
        recyclerview.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MyChangeResponse.DataBean d = (MyChangeResponse.DataBean) adapter.getItem(position);
                Intent intent = new Intent(MyJfdhActivity.this, JfOrderDetailActivity.class);
                intent.putExtra("orderid",d.getId()+"");
                startActivity(intent);
            }
        });

        getList();
    }

    @Override
    protected void setListener() {

        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                getList();
            }
        });
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                getList();
            }
        });
    }


    @OnClick({R.id.title_finish, R.id.tv_wfh, R.id.tv_yfh})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_finish:
                finish();
                break;
            case R.id.tv_wfh:
                tvWfh.setSelected(true);
                tvYfh.setSelected(false);
                type = 0;
                page = 1;
                getList();
                break;
            case R.id.tv_yfh:
                tvWfh.setSelected(false);
                tvYfh.setSelected(true);
                type = 1;
                page = 1;
                getList();
                break;
        }
    }

    private int type = 0;

    /**
     * 获取精选
     */
    private void getList() {
        OkHttpUtils.post()
                .url(UrlHost.FEN_HISTORY)
                .addParams("uid", SharePrefUtils.getUserId())
                .addParams("page", page + "")
                .addParams("type", type + "")//0未发货  1已发货
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                    multStatusView.showError();
                    reset();
                }
            }

            @Override
            public void onResponse(String response, int id) {
                reset();
                if (response != null) {
                    MyChangeResponse myChangeResponse = JsonUtils.parseByGson(response, MyChangeResponse.class);
                    if (myChangeResponse == null) {
                        return;
                    }
                    if (CallbackCode.SUCCESS.equals(myChangeResponse.getCode())) {
                        List<MyChangeResponse.DataBean> list = myChangeResponse.getData();
                        if (page == 1) {
                            multStatusView.showContent();
                            if (list != null && !list.isEmpty()) {
                            } else {
                                multStatusView.showEmpty();
                            }
                            adapter.setNewData(list);
                        } else {
                            adapter.addData(list);
                        }
                    } else {
                        if (page == 1) {
                            multStatusView.showEmpty();
                        }
                    }
                } else {
                    multStatusView.showError();
                }
            }
        });
    }

    private void reset() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh(true);
        }
        refreshLayout.finishLoadmore();
    }

    class JfDuiHuanAdapter extends BaseQuickAdapter<MyChangeResponse.DataBean, BaseViewHolder> {
        public JfDuiHuanAdapter(int layoutResId) {
            super(layoutResId);
        }

        public void clear() {
            getData().clear();
            notifyDataSetChanged();
        }

        @Override
        protected void convert(BaseViewHolder helper, MyChangeResponse.DataBean item) {
            helper.setText(R.id.name, item.getName());
            ViewUtils.loadImage(mContext, item.getPic(), (ImageView) helper.getView(R.id.image));
            helper.setText(R.id.status, "积分:" + item.getScore());

            TextView time = helper.getView(R.id.time);
            if (type == 0) {
                time.setVisibility(View.INVISIBLE);
                time.setText("");
            } else {
                time.setVisibility(View.VISIBLE);
                time.setText(item.getAddtime());
            }
        }
    }
}
